﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using PassBag.Utils;
using PassBag.Logic.Backup;


namespace TestPassBag {
    
    [TestFixture, Description ("Test ZipTools class")]
    public class TesBackuptFile {
        private const string OUTPUT_DIRECTORY = "zipoutput";
        private const string OUTPUT_FILE = "file{0}.txt";
        private const string LONG_OUTPUT_FILE = "filelong{0}.txt";
        private const int LONG_FILE_SIZE = Int16.MaxValue;
        private const string ZIP_FILE_1 = "zip1.zip";
        private const string ZIP_FILE_2 = "zip2.zip";
        private const string ZIP_FILE_LONG = "ziplong.zip";

        private readonly string[] FILE_CONTENT_ARRAY = {"I am hugo !", 
                                                     "Hello from passbag", 
                                                     "This software is\namazing\nincredible",
                                                     "What are you doing ?\nI am using passbag",
                                                     "Open source software open's your mind"};
        private readonly char[] LONG_FILE_CONTENT_ARRAY = {'a', 
                                                     'b', 
                                                     'c',
                                                     'd',
                                                     'e'};
        private readonly string[] FILE_COMMENT_ARRAY = {"Comment:I am hugo !", 
                                                     "Comment:Hello from passbag", 
                                                     "Comment:This software is\namazing\nincredible",
                                                     "Comment:What are you doing ?\nI am using passbag",
                                                     "Comment:Open source software open's your mind"};
        private const int NB_FILES = 5;

        [SetUp, Description ("Prepare directory for output and create txt files")]
        public void PrepareZipOutputDirectory () {
            if (System.IO.Directory.Exists (OUTPUT_DIRECTORY)) {
                System.IO.Directory.Delete (OUTPUT_DIRECTORY, true);
            }
            System.IO.Directory.CreateDirectory (OUTPUT_DIRECTORY);

            int i = 0;
            foreach (string item in FILE_CONTENT_ARRAY) {
                System.IO.File.WriteAllText (GetFileNameHelper (i), FILE_CONTENT_ARRAY[i]);
                i++;
            }

            for (i = 0; i < NB_FILES; i++) {
                System.IO.File.WriteAllText (GetLongFileNameHelper (i), new String (LONG_FILE_CONTENT_ARRAY[i], LONG_FILE_SIZE));
            }
        }

        [TearDown, Description ("Clear directory content")]
        public void ClearZipOutputDirectory () {
            if (System.IO.Directory.Exists (OUTPUT_DIRECTORY)) {
                System.IO.Directory.Delete (OUTPUT_DIRECTORY, true);
            }
        }

        [Test, Description ("Zip files with password, add item to existing file and read entries")]
        public void ZipEnumerateEntriesAndCheck () {
            List<BackupZipEntry> list = new List<BackupZipEntry>();

            for (int i = 0; i < NB_FILES - 1; i++) {
                list.Add (new BackupZipEntry (GetFileNameHelper (i), FILE_COMMENT_ARRAY[i]));
			}

            BackupSet.CreateZipFiles (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_1, list);

            BackupZipEntry addFile = new BackupZipEntry (GetFileNameHelper (NB_FILES - 1), FILE_COMMENT_ARRAY[NB_FILES - 1]);

            BackupSet.AddFileToArchive (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_1, addFile);

            List<BackupZipEntry> backupFileList = BackupSet.GetBackupFleList (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_1);

            int j = 0;

            Assert.AreEqual (backupFileList.Count, NB_FILES);
            
            foreach (BackupZipEntry item in backupFileList) {

                Assert.IsTrue(item.FileName.EndsWith( string.Format (OUTPUT_FILE, j)));
                Assert.AreEqual( item.Comment, FILE_COMMENT_ARRAY[j]);

                j++;
            }
        }


        [Test, Description ("Zip/Unzip files with password and read entries")]
        public void ZipUnzipAndCheck () {
            List<BackupZipEntry> list = new List<BackupZipEntry> ();

            for (int i = 0; i < NB_FILES; i++) {
                list.Add (new BackupZipEntry (GetFileNameHelper (i), FILE_COMMENT_ARRAY[i]));
            }

            BackupSet.CreateZipFiles (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_2, list);

            // delete txt files
            for (int i = 0; i < NB_FILES; i++) {
                System.IO.File.Delete (GetFileNameHelper (i));
            }

            List<BackupZipEntry> backupFileList = BackupSet.GetBackupFleList (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_2);
            int j =  0;

           // extract files
            foreach (BackupZipEntry item in backupFileList) {

                Assert.IsTrue (BackupSet.ExtractZipEntry (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_2, item.FileName, GetFileNameHelper (j)));

                j++;
            }

            // test file content
            for (int i = 0; i < NB_FILES; i++) {
                Assert.AreEqual(System.IO.File.ReadAllText (GetFileNameHelper (i)), FILE_CONTENT_ARRAY[i]) ;
            }            
        }

        [Test, Description ("Zip/Unzip files with password and read entries")]
        public void LongZipUnzipAndCheck () {
            List<BackupZipEntry> list = new List<BackupZipEntry> ();

            for (int i = 0; i < NB_FILES; i++) {
                list.Add (new BackupZipEntry (GetLongFileNameHelper (i), FILE_COMMENT_ARRAY[i]));
            }

            BackupSet.CreateZipFiles (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_LONG, list);

            // delete txt files
            for (int i = 0; i < NB_FILES; i++) {
                System.IO.File.Delete (GetLongFileNameHelper (i));
            }

            List<BackupZipEntry> backupFileList = BackupSet.GetBackupFleList (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_LONG);
            int j = 0;

            // extract files
            foreach (BackupZipEntry item in backupFileList) {

                Assert.IsTrue (BackupSet.ExtractZipEntry (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_LONG, item.FileName, GetLongFileNameHelper (j)));

                j++;
            }


            //// test file extract
            //for (int i = 0; i < NB_FILES; i++) {
            //    Assert.IsTrue (BackupSet.ExtractZipEntry (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + ZIP_FILE_LONG, string.Format (LONG_OUTPUT_FILE, i), GetLongFileNameHelper (i)));
            //}

            // test file content
            for (int i = 0; i < NB_FILES; i++) {
                Assert.AreEqual (System.IO.File.ReadAllText (GetLongFileNameHelper (i)), new String (LONG_FILE_CONTENT_ARRAY[i], LONG_FILE_SIZE));
            }
        }

        private string GetLongFileNameHelper (int index) {
            return OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + string.Format (LONG_OUTPUT_FILE, index);
        }

        private string GetFileNameHelper (int index) {
            return OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + string.Format (OUTPUT_FILE, index);
        }
    }
}
